$(function () {
    var code = "";
    if (window.location.href.indexOf("code") != -1) {
        code = window.location.href.split("code=")[1].split("&state")[0];
    }
    $.ajax({
        url: Feng.ctxPath + "/integralMallWeixin/getScoreShopping",
        type: "POST",
        data: {
            "code": code,
            "area": "es"
        },
        success: function (data) {
            if (data == '' || data == null) {
                window.location.href = Feng.ctxPath + "/integralMallWeixin/logincus";
                return false;
            }
            var html = "", text = "";
            var infoList = data.listPointRewordInfo;
            html += '<div class="integralMallHeadTop"><div>积分商城</div></div>' +
                '<div style="padding: 10px 0 20px;height: calc(100% - 45px);overflow: auto;">' +
                '       <div class="integralMallHeader">' +
                '            <input type="hidden" id="reservationsId" value="' + data.reservationsId + '">' +
                '            <div class="HeaderPortrait" id="indexcus">' +
                '                <div class="HeaderCircle">' +
                '                    <img src="https://pynnn.oss-cn-hangzhou.aliyuncs.com/one/QQ20190826112636.jpg" style="width: 100%;border-radius: 50%;">' +
                '                </div>' +
                '                <div class="HeaderTelephone">' +
                '                    <div style="color: #AAAAAA">' + data.account + '</div>' +
                '                </div>' +
                '            </div>' +
                '            <div style="text-align: right;"><span style="font-size: 26px;color: black;">' + data.scoreBalance + '</span><span style="vertical-align: text-bottom;">积分</span></div>' +
                '        </div>' +
                '       <div class="integralMallNav">' +
                '            <div id="integralRecord">' +
                '                <img src="https://pynnn.oss-cn-hangzhou.aliyuncs.com/one/IntegralRecord.png" alt="">' +
                '                <div>兑换记录</div>' +
                '            </div>' +
                '            <div id="integralRule">' +
                '                <img src="https://pynnn.oss-cn-hangzhou.aliyuncs.com/one/IntegralRule.png" alt="">' +
                '                <div>积分规则</div>' +
                '            </div>' +
                '        </div>' +
                '       <div class="integralMallContent"></div>' +
                '</div>' +
                '<div class="integralMallFoot">' +
                '   <button class="btn btn-primary btn-lg btn-block" type="button">查看更多</button>' +
                '</div>';
            for (var i = 0; i < infoList.length; i++) {
                text += '<div class="thumbnail">' +
                    '        <img src="' + infoList[i].imagePath + '" alt="..." style="width: 100%;" onclick="commodityDetails(' + infoList[i].id + ')">' +
                    '        <div class="caption" style="padding-right: 0;">' +
                    '            <div>' + infoList[i].productName + '</div>' +
                    '            <div style="font-size: 12px;color: #CBCBCB;"><span style="color: #022871;font-size: 16px;margin-right: 10px;">积分:' + infoList[i].productPrice + '</span><span>市场价:￥' + infoList[i].marketPrice + '</span></div>' +
                    '            <div style="font-size: 12px;color: #CBCBCB;"><span style="margin-right: 20px;">已兑换:' + infoList[i].conversionedNumber + '个</span><span>剩余:' + infoList[i].putawayNumber + '个</span></div>' +
                    '            <div><span style="color: #00A8CB" class="edits" onclick="conversion(' + "'" + infoList[i].id + "','" + infoList[i].productName + "','" + infoList[i].productPrice + "'" + ')">兑换</span></div>' +
                    '        </div>' +
                    '    </div>'
            }
            $(".integralMall").html(html);
            $(".integralMall").find('.integralMallContent').html(text);
        },
        error: function (data) {

        }
    });
    $(".integralMall").on('click','#integralRule',function () {
        window.location.href = Feng.ctxPath + "/integralMallWeixin/integralRule";
    });
    $(".integralMall").on('click','#integralRecord',function () {
        window.location.href = Feng.ctxPath + '/integralMallWeixin/integralRecord/' + "2";
    });
    $(".integralMall").on('click','#indexcus',function () {
        window.location.href = Feng.ctxPath + "/integralMallWeixin/personalCenter";
    });
});

function conversion(id, productName, productPrice) {
    var reservationsId = $('#reservationsId').val();
    var numAcount;
    if (reservationsId == "" || reservationsId == null || reservationsId == undefined
        || reservationsId == "null" || reservationsId == "undefined") {
        Feng.success("请先登录!");
        window.location.href = Feng.ctxPath + "/integralMallWeixin/logincus";
        return false;
    }
    $.ajax({
        url: Feng.ctxPath + "/integralMallWeixin/checkPrice",
        type: "post",
        data: {
            "id": id,
            "reservationsId": reservationsId,
            "productPrice": productPrice
        },
        success: function (data) {
            if (data == -2) {
                alert("您的积分不够兑换此商品！")
            } else if (data == -1) {
                alert("库存为0，请选择其他商品！")
            } else {
                if (confirm("是否确定花费" + productPrice + "积分，兑换:" + productName + "？")) {
                    numAcount = prompt("本月最多可兑换:" + data + "张", "")
                    if (numAcount == null || numAcount == "") {
                        return false;
                    }
                    if (isNaN(numAcount)) {
                        alert("请输入数字！")
                        return false;
                    }
                    if(numAcount>data){
                        alert("最多只能兑换"+data+"张！")
                        return false;
                    }
                    $.ajax({
                        url: Feng.ctxPath + "/integralMallWeixin/conversion",
                        type: "post",
                        data: {
                            "id": id,
                            "reservationsId": reservationsId,
                            "numAcount": numAcount
                        },
                        success: function (data) {
                            if (data == 9) {
                                Feng.success("请先登录!");
                                window.location.href = Feng.ctxPath + "/integralMallWeixin/logincus";
                            } else {
                                Feng.success('操作成功!');
                                window.parent.parent.location.href = Feng.ctxPath + '/integralMallWeixin/integralRecord/' + "2";
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            }
                        },
                        error: function (data) {
                            alert('权限被限制，请联系管理员!');
                        }
                    });
                }
            }
        }
    });
}

function commodityDetails(id) {
    window.location.href = Feng.ctxPath + "/integralMallWeixin/commodityDetails/" + id;
}